package com.base.cn.platform.os.controller.user.gradeconfig;

import com.base.cn.platform.os.common.AdminBaseController;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.service.manage.user.gradeconfig.CusUserGradeConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 后台等级管理
 *
 * @author ll
 * @create 2018-04-09-19:08
 */
@Controller
@RequestMapping("/admin/user/cus")
public class AdminCusUserGradeConfigController extends AdminBaseController {

    @Autowired
    private CusUserGradeConfigService cusUserGradeConfigService;

    /**
     * 初始化列表页面
     * @return
     */
    @RequestMapping("/gradeConfigList")
    public String gradeConfigList(){
        return resultPage("/user/gradeconfig/gradeconfig_list");
    }

    /**
     * 加载数据列表
     * @param model Model
     * @return 列表页面
     */
    @RequestMapping("/ajaxCusUserGradeConfigList")
    public String ajaxCusUserGradeConfigList(Model model,
                               @RequestParam(value = "currentPage",defaultValue = "1",required = false) int currentPage){
        Map<String,Object> dataMap = cusUserGradeConfigService.queryCusUserGradeConfigList(currentPage);
        model.addAttribute("dataMap",dataMap);
        return resultPage("/user/gradeconfig/gradeconfig_ajax_list");
    }


    /**
     * 去添加修改用户等级信息页面
     * @param model
     * @param gradeConfigId
     * @return
     */
    @RequestMapping("/saveCusUserGradeConfig")
    public String toAddUpdateGradeConfig(Model model,
                                         @RequestParam(value = "gradeConfigId",required = false,defaultValue = "")BigDecimal gradeConfigId ){
        Map<String,Object> userGradeConfig=new HashMap<>();
            if(DataUtil.idIsNotNull(gradeConfigId)){
                userGradeConfig = cusUserGradeConfigService.queryGradeConfigById(gradeConfigId);
            }else {
                List<Map<String,Object>> userGradeConfigs = cusUserGradeConfigService.queryCusUserGradeConfigAll();
                model.addAttribute("gradeName","LV"+(userGradeConfigs.size()+1));
            }
            model.addAttribute("userGradeConfig",userGradeConfig);
            model.addAttribute("gradeConfigId",gradeConfigId);
        return resultPage("/user/gradeconfig/gradeconfig_creat");
    }

    /***
     * 添加学员
     * @return
     */
    @RequestMapping("/ajaxSaveCusUserGradeConfig")
    @ResponseBody
    public Map<String,Object>  ajaxSaveCusUserGradeConfig(@RequestParam("id") BigDecimal id,
                                                          @RequestParam("gradeName") String gradeName,
                                                          @RequestParam("gradeTitle") String gradeTitle,
                                                          @RequestParam("experience") Long experience){
        //用户数据
        Map<String,Object> paramsMap =new HashMap<>();
        paramsMap.put("id",id);
        paramsMap.put("gradeName",gradeName);
        paramsMap.put("gradeTitle",gradeTitle);
        paramsMap.put("experience",experience);
        Map<String,Object> dataMap =cusUserGradeConfigService.saveCusUserGradeConfig(paramsMap);
        return dataMap;
    }
}
